// pages/guangchang/guangchang.js
const db = wx.cloud.database()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    position: [],
    imgsrc: ["cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/海报_画板 1.png", "cloud://gaogaolo-qy3zl.6761-gaogaolo-qy3zl-1304238148/海报2_画板 1.png"],
    jihua: [],
    jihua2: [],
    jihua3: [],
    jihuaall: [],
    flag: true,
    flag2: false,
    flag3: true,
    flagall: "",
    color: "#ffffff",
    color1: "#D6F4FF",
    color2: "#ffffff",
    color3: "#FEFEDF",
    list: [],
    list2: [],
    list3: [],
    flagall:'',
    limit: 4 ,//每次拉取数量
    use1:[],
    use2:[],
    use3:[]

  },

  yundong: function () {
    if (this.data.flag) {
      this.data.flag = false
      this.data.flag2 = true
      this.data.flag3 = true
      this.data.flagall = true
    }
    // else{
    //   this.data.flag = true
    //   this.data.flag2 = true
    //   this.data.flag3 = true
    //   this.data.flagall = false
    // }
    this.setData({
      flag: this.data.flag,
      flag2: this.data.flag2,
      flag3: this.data.flag3,
      flagall: this.data.flagall
    })
    console.log(this.data.flagall)
  },

  xuexi: function () {
    if (this.data.flag2) {
      this.data.flag2 = false
      this.data.flag = true
      this.data.flag3 = true
      this.data.flagall = true
    }
    // else{
    //   this.data.flag2 = true
    //   this.data.flag = true
    //   this.data.flag3 = true
    //   this.data.flagall = false
    // }
    this.setData({
      flag: this.data.flag,
      flag2: this.data.flag2,
      flag3: this.data.flag3,
      flagall: this.data.flagall
    })
    console.log(this.data.flagall)
  },

  shenghuo: function () {
    if (this.data.flag3) {
      this.data.flag3 = false
      this.data.flag = true
      this.data.flag2 = true
      this.data.flagall = true
    }
    // else{
    //   this.data.flag3 = true
    //   this.data.flag = true
    //   this.data.flag2 = true
    //   this.data.flagall = false
    // }
    this.setData({
      flag: this.data.flag,
      flag2: this.data.flag2,
      flag3: this.data.flag3
    })
    console.log(this.data.flagall)
  },

  details: function (e) {
    console.log("details", e)
    console.log("detailsid", e.currentTarget.dataset.id)
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/particulars/particulars?id=' + id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  getpaiming: function () {
    db.collection("user").orderBy("number","desc").limit(3).get({
        success:res=>{
          console.log("排名",res)
          var data1 = res.data[0]
          var data2 = res.data[1]
          var data3 = res.data[2]
          this.setData({
            use1:data1,
            use2:data2,
            use3:data3
          })
          console.log(this.data.use1)
          console.log(this.data.use2)
          console.log(this.data.use3)
        }
      })
  },

  getData: function () {
    db.collection("jihua")
      .where({
        leixing: "学习",
        flag: true,
        finish: false
      }).skip(this.data.list.length).limit(this.data.limit).get().then(res => {
        this.setData({
          list: [...this.data.list, ...res.data], //合并数据
          isEndOfList: res.data.length < this.data.limit ? true : false //判断是否结束
        })
        console.log("学习数据", this.data.list)
      })
  },

  getData2: function () {
    db.collection("jihua")
      .where({
        leixing: "运动",
        flag: true,
        finish: false
      }).skip(this.data.list2.length).limit(this.data.limit).get().then(res => {
        this.setData({
          list2: [...this.data.list2, ...res.data], //合并数据
          isEndOfList: res.data.length < this.data.limit ? true : false //判断是否结束
        })
        console.log("运动数据", this.data.list2)
      })
  },

  getData3: function () {
    db.collection("jihua")
      .where({
        leixing: "生活",
        flag: true,
        finish: false
      }).skip(this.data.list3.length).limit(this.data.limit).get().then(res => {
        this.setData({
          list3: [...this.data.list3, ...res.data], //合并数据
          isEndOfList: res.data.length < this.data.limit ? true : false //判断是否结束
        })
        console.log("生活数据", this.data.list3)
      })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    db.collection("jihua").doc("133e253361c098a2014d34b24c6bfc2d").get({
      success: res => {
        var flagall=res.data.flag
        this.setData({
          flagall:flagall
        })
      }
    })
    this.setData({
      list:[],
      list2:[],
      list3:[]
    })
    this.getData()
    this.getData2()
    this.getData3()
    this.getpaiming()
    wx.stopPullDownRefresh()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow();
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.isEndOfList || this.getData()
    this.data.isEndOfList || this.getData2()
    this.data.isEndOfList || this.getData3()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})